<?php
class Frmcreator_AdminSettingsController extends Core_Controller_Action_Admin
{
    public function indexAction()
    {
        $this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')->
            getNavigation('frmcreator_admin_main', array(), 'frmcreator_admin_main_settings');

        $this->view->form = $form = new Frmcreator_Form_Admin_Global();

        $api = Engine_Api::_()->frmcreator();        
        $lstForms = $api->getSelectForms();
        
        $oldSelectFormId = Engine_Api::_()->getApi('settings', 'core')->getSetting('frmcreator.selectform',
        "0");
        
        $form->frmcreator_selectform->setMultiOptions($lstForms);

        if ($this->getRequest()->isPost() && $form->isValid($this->_getAllParams())) {
            $values = $form->getValues();
            $table = Engine_Api::_()->getDbtable('menuItems', 'core');
            $select = $table->select();
            $select->where('name = ?', 'core_footer_contact');
            $contactMenu = $table->fetchRow($select);
            foreach ($values as $key => $value) {
                Engine_Api::_()->getApi('settings', 'core')->setSetting($key, $value);
                if($key == "frmcreator_selectform"){
                    if($value == "0"){
                        if($contactMenu){
                            $contactMenu->enabled = 1;
                            $contactMenu->save();
                        }
                        
                        //Restore default Contact form
                        $oldSelectForm = Engine_Api::_()->getItem('frmcreator_form', $oldSelectFormId);
                        if($oldSelectForm){
                            $menu_form_name = "frmcreator_footer_" . $oldSelectForm->url;
                            $select = $table->select();
                            $select->where('name = ?', $menu_form_name);
                            $oldformMenu = $table->fetchRow($select);
                            if($oldformMenu){
                                $oldformMenu->label = $oldSelectForm->title;
                                $oldformMenu->save();
                            } 
                        }
                        
                    } else {
                        //Replace contact form
                        if($contactMenu){
                            $contactMenu->enabled = 0;
                            $contactMenu->save();
                        }
                        
                        $newform = Engine_Api::_()->getItem('frmcreator_form', $value);
                        if($newform){
                            $menu_form_name = "frmcreator_footer_" . $newform->url;
                            $select = $table->select();
                            $select->where('name = ?', $menu_form_name);
                            $formMenu = $table->fetchRow($select);
                            if($formMenu){
                                $formMenu->label = "Contact";
                                $formMenu->save();
                            } else {
                                //Create a menu if it's not exist
                            }
                        }
                    }
                }
                
            }
            $form->addNotice('Your changes have been saved.');
        }
    }
}
